﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ISRecord.View.ViewsModel;
using ISRecord.View.Services;
using ISRecord.Model;
using GalaSoft.MvvmLight.Messaging;

namespace ISRecord.View.Views
{
    /// <summary>
    /// Interaction logic for OutboxView.xaml
    /// </summary>
    public partial class OutboxView : UserControl
    {
        OutboxViewModel vm;
        public OutboxView()
        {
             InitializeComponent();
             vm = new OutboxViewModel();
             this.DataContext = vm;
        }

        private void WatchForEnterOutboxNo(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                TextBox tb = (TextBox)sender;
                if (string.IsNullOrEmpty(tb.Text))
                {
                    StartEdit();
                }
                else if (!string.IsNullOrEmpty(tb.Text))//Edit What this number represent.
                {
                    StartEdit(tb.Text);
                }
                e.Handled = true;
                txtOutboxNo.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }


        }

        private void StartEdit()
        {
            vm.CreateOutbox();

        }
        private void StartEdit(string id)
        {
            vm.FindOutbox(id);
        }

        #region "Key Down event watches for enetering date slash"

        private void WatchForDateBoxKeyDown(object sender, KeyEventArgs e)
        {
            SendSapceKeyStroke((Control)sender, e);
        }

        private static void SendSapceKeyStroke(Control sender, KeyEventArgs e)
        {
            if (e.Key == Key.Divide || e.Key == Key.OemQuestion)
            {
                Helper.SendKeyStroke(sender, Key.Space);
            }
        }
        #endregion

        private void ViewLoaded(object sender, RoutedEventArgs e)
        {
            this.txtOutboxNo.Focus();
            Messenger.Default.Register<ModelState>(this, state => RespondToModelStateChanged(state));
            txtOutboxNo.Focus();
        }
        private void RespondToModelStateChanged(ModelState state)
        {
            switch (state)
            {
                case ModelState.NotLoaded:
                    break;
                case ModelState.Constructed:
                    break;
                case ModelState.Founded:
                    break;
                case ModelState.Deleted:
                    txtOutboxNo.Focus();
                    break;
                case ModelState.Cleared:
                    txtOutboxNo.Focus();
                    break;
                default:
                    break;
            }
        }

        private void ViewClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (vm.ModelHasChanges)
            {
                if (!Helper.ShowMessage(Properties.Resources.PrompetToSaveMsg, MessagePrompetType.AskQuestion))
                {
                    e.Cancel = true;
                    return;
                }
            }
            Messenger.Default.Unregister<ModelState>(this, state => RespondToModelStateChanged(state));
            vm.Cleanup();

        }

        private void ContenGridKeyDown(object sender, KeyEventArgs e)
        {
            //First Check if source of the event is DraftNo textbox, if so return, as it will be handled by that one.
            var uie = e.OriginalSource as UIElement;
            var textbox = uie as TextBox;
            if (textbox != null && textbox.Name == txtOutboxNo.Name || textbox.AcceptsReturn)
            {
                return;
            }
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                uie.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

        private void ViewUnloaded(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Unregister<ModelState>(this, state => RespondToModelStateChanged(state));
        }

    }
}
